<template>
<div class="container">
    <div class="page-head">
        <div class="left">
            <el-form :inline="true" ref="form" :model="query" @submit.native.prevent>
                <el-form-item>
                    <el-button class="filter-item" type="primary" @click="showAddView">添加</el-button>
                </el-form-item>
                <el-form-item>
                    <el-input v-model="query.keyword" placeholder="公司名称" style="width: 200px" class="filter-item" clearable @clear="doSearch" />
                </el-form-item>
                <el-button class="filter-item" type="primary" native-type="submit" @click="doSearch">搜索</el-button>
            </el-form>
        </div>
    </div>

    <el-table ref="tableView" height="tableHeight" class="page-body" :data="tableData" element-loading-text="Loading" border fit highlight-current-row>
        <el-table-column label="ID" align="center" width="95">
            <template slot-scope="scope">
                {{ scope.row.id }}
            </template>
        </el-table-column>
        <el-table-column label="公司简称" prop="short_name" width="120"></el-table-column>
        <el-table-column label="公司名称">
            <template slot-scope="scope">
                {{ scope.row.title }}
            </template>
        </el-table-column>
        <el-table-column label="排序" prop="sort" width="100px"></el-table-column>
        <el-table-column label="操作" align="center" width="200px">
            <template slot-scope="scope">
                <el-button type="primary" size="mini" @click="showEditView(scope.row)">编辑</el-button>
                <el-button type="danger" size="mini" @click="deleteRow(scope.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>
    <div class="page-foot">
        <div class="left"></div>
        <div class="right">
            <common-pagination :total="pg.total" :page.sync="pg.page" :limit.sync="pg.limit" @getList="getTableData"></common-pagination>
        </div>
    </div>

    <!-- 页面组件 -->
    <edit-view ref="editView" @getTableData="getTableData"></edit-view>
</div>
</template>

<script>
import {
    getTableDataApi,
    deleteCompanyApi,
} from "@/api/system/dictionary/company";

import pagination from "@/components/mixins/pagination.vue";
import editView from "./edit.vue";

export default {
    name: "system_dictionary_company",
    components: {
        editView
    },
    mixins: [pagination],
    data() {
        return {
            query: {
                keyword: "",
            },
            tableData: []
        };
    },
    created() {
        this.getTableData();
    },
    methods: {
        doSearch(){
            this.resetPg();
            this.getTableData();
        },
        getTableData() {
            let params = Object.assign({}, this.query);
            params["pagination"] = this.pg;

            getTableDataApi(params).then((res) => {
                if (res.code == 200) {
                    this.tableData = res.data.list;
                    this.setPg(res);
                } else {
                    this.$message.warning(res.msg);
                }
            });
        },
        // 添加公司
        showAddView() {
            if (this.$refs["editView"] != undefined) {
                this.$refs["editView"].showAddView();
            } else {
                this.$nextTick(function () {
                    this.$refs["editView"].showAddView();
                });
            }
        },
        showEditView(row) {
            if (this.$refs["editView"] != undefined) {
                this.$refs["editView"].showSaveView(row);
            } else {
                this.$nextTick(function () {
                    this.$refs["editView"].showSaveView(row);
                });
            }
        },
        deleteRow(row) {
            this.$confirm("确认删除该条信息?", "警告", {
                    type: "warning",
                })
                .then((res) => {
                    deleteCompanyApi(row)
                        .then((res) => {
                            if (res.code == 200) {
                                this.getTableData();
                                this.$message.success(res.msg);
                            } else {
                                this.$message.error(res.msg);
                            }
                        })
                        .catch(res => {
                            this.$message.info("系统异常，请联系管理员");
                        });
                })
                .catch((res) => {
                    this.$message.info("您取消了删除操作");
                });
        },
    },
};
</script>
